package com.lonwin.mes2u8.util;

import com.alibaba.fastjson.JSONObject;
import com.lonwin.mes2u8.common.CommonValue;
import com.lonwin.mes2u8.domain.u8.DefineSyncResponse;
import com.lonwin.mes2u8.domain.u8.U8CommonAddResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.Objects;
import java.util.Queue;

/**
 * 响应信息队列操作相关工具类
 * @author DELL
 * @Date 2023-12-22 18:21
 */
@Slf4j

public class MyQueueUtil {

    /**
     * 判断队列中的异步消息是否是成功的，如果不全是，则返回失败的信息列表
     * @param queue 待判断队列
     * @return 失败队列，待判断队列为空或者都为成功则返回空
     * @param <T> 为DefineSyncResponse的子类
     */
    public static <T extends DefineSyncResponse> Queue<T> checkQueueSuccess(Queue<T> queue) {

        Queue<T> failedQueue = new LinkedList<>();

        while(!queue.isEmpty()) {
            T queueHead = queue.peek();
            if (Objects.nonNull(queueHead) && LocalDateTime.now().isAfter(queueHead.getOffTime())) {
                log.info("MyQueueUtil.checkQueueSuccess send url");
                if (StringUtils.isBlank(queueHead.getUrl())) {
                    queue.poll();
                    continue;
                }
                String respStr = MyHttpClientUtil.sendGetRequest(queueHead.getUrl(), "", MyHttpClientUtil::handleJsonResponse);

                U8CommonAddResult u8CommonAddResult = JSONObject.parseObject(respStr, U8CommonAddResult.class);
                log.info("MyQueueUtil.checkQueueSuccess parse result : {}", u8CommonAddResult);
                if (Objects.isNull(u8CommonAddResult) || !CommonValue.U8_RESPONSE_SUCCESS.equals(u8CommonAddResult.getErrcode())) {
                    queueHead.setSuccess(false);
                    queueHead.setMessage(Objects.isNull(u8CommonAddResult) ? "null" : u8CommonAddResult.getErrmsg());
                    failedQueue.add(queueHead);
                }
                queue.poll();
            }

        }
        return failedQueue;
    }

    public static DefineSyncResponse isResponseSuccess(DefineSyncResponse response) {
        log.info("response : {}", response.toString());

        if (StringUtils.isBlank(response.getUrl())) {
            return null;
        }

        while(LocalDateTime.now().isBefore(response.getOffTime())) {
            log.info("MyQueueUtil.isResponseSuccess time too early");
        }
        String respStr = MyHttpClientUtil.sendGetRequest(response.getUrl(), "", MyHttpClientUtil::handleJsonResponse);

        U8CommonAddResult u8CommonAddResult = JSONObject.parseObject(respStr, U8CommonAddResult.class);
        log.info("MyQueueUtil.checkQueueSuccess parse result : {}", u8CommonAddResult);
        if (Objects.isNull(u8CommonAddResult) || !CommonValue.U8_RESPONSE_SUCCESS.equals(u8CommonAddResult.getErrcode())) {
            response.setSuccess(false);
            response.setMessage(Objects.isNull(u8CommonAddResult) ? "null" : u8CommonAddResult.getErrmsg());
            return response;
        }

        return null;
    }
}
